<?php


require_once 'shared-functions.php';

if ($argc != 7)
{
    echo "Usage:\tphp $argv[0] username password firstname middlename lastname role\n";
    exit(1);
}
$hash = gen_passwd_hash($argv[2]);

echo "'$hash' is the hash for '$argv[1]'\n";

$link = connect_db();
$query = "SELECT `UserId` FROM `User` WHERE `UserId` LIKE 'EM%' ORDER BY  `User`.`UserId` DESC;";
$result = mysql_query($query,$link);
$last_user = mysql_fetch_array($result,MYSQL_BOTH);
$last_user = substr($last_user['UserId'],2);
$new_user = str_pad($last_user+1,6,'0',STR_PAD_LEFT);

$query ="INSERT INTO `User`
         (
             `UserId`, `UserName`, `Password`, `PrivilegeLevel`,
             `FirstName`, `MiddleName`, `LastName`
         )
         VALUES
         (
             'EM".$new_user."', '".$argv[1]."', '".$hash."',
             (
                 SELECT `PrivilegeLevel`
                 FROM `Privilege`
                 WHERE `GroupName` = '".$argv[6]."'
                 LIMIT 1
             ),
             '".$argv[3]."', '".$argv[4]."', '".$argv[5]."'
         );";
echo $query."\n";

$result = mysql_query($query,$link);

echo $result."\n";
?>
